package com.zx._03_并发.chapter_06_compent;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import lombok.extern.slf4j.Slf4j;


/**
 * 同步组件——Condition
 */
@Slf4j
public class Demo_07_Condition {

    public static void main(String[] args) {
        ReentrantLock reentrantLock = new ReentrantLock();
        Condition condition = reentrantLock.newCondition();

        new Thread(() -> {
            reentrantLock.lock();
            try {
                log.info("wait signal"); // 1
                condition.await();//进入等待状态，等待condition的信号
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                reentrantLock.unlock();
            }
            log.info("get signal"); // 4
        }).start();

        new Thread(() -> {
            reentrantLock.lock();
            try {
                log.info("get lock"); // 2
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                reentrantLock.unlock();
            }
            condition.signalAll();//通知所有的condition，可以运行了
            log.info("send signal ~ "); // 3
        }).start();
    }
}
